package com.firefly.mobile.app.util;

import java.util.concurrent.LinkedBlockingQueue;

import android.util.Log;

/**
 * 日志工具类
 */
public class SytLog {
	
	private static LinkedBlockingQueue<String> logMsgQueue = new LinkedBlockingQueue<String>();
	
	
	private static boolean clean =  false;
	
	public static void i(String msg){
		i("common", msg);
	}
	
	public static void e(String msg){
		e("common", msg);
	}
	
	public static void e(String msg,Throwable e){
		e("common", msg+"\n"+e.getStackTrace());
	}
	
	public static void i(String tag,String msg){
		//日志写入SDCARD中
//		store("info",tag,msg);  
		//测试使用
		Log.i(tag, msg);
	}
	
	public static void e(String tag,String msg){
		//日志写入SDCARD中
//		store("error",tag,msg);  
		//测试使用
		Log.e(tag, msg);
		
	}
	
//	private static void store(String level,String tag,String msg) {
//		String status = Environment.getExternalStorageState();
//		if (!status.equals(Environment.MEDIA_MOUNTED)) {
//			return;
//		}
//		String date = new SimpleDateFormat("yyyyMMdd",Locale.getDefault()).format(new Date());
//		String time = new SimpleDateFormat("HH:mm:ss",Locale.getDefault()).format(new Date());
////		String data = time+" "+"["+level+"]\t"+ new Throwable().getStackTrace()[1].getClassName()+"(" 
////				+ new Throwable().getStackTrace()[1].getMethodName() + "," + new Throwable().getStackTrace()[1].getLineNumber() + ") " + msg;
//		String data = time+" "+"["+level+"]\t"+ msg;
//		String logFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath()+LogInfo.LOG_FILE_PATH;
//		createDirs(logFolderPath);
//		String fileName = logFolderPath+File.separator+"syt-app"+"_"+date+".log";
//		File file = new File(fileName);
//		
//		try {  
//			if (!file.exists()) {
//				file.createNewFile();
//			}
//			if (file.length() > Long.parseLong(LogInfo.LOG_MAX_LENGTH+"000000")) {
//				Log.e("SytLog", "日志文件超过最大限制，无法写入");
//				return;
//			}
//            FileWriter filerWriter = new FileWriter(file, true);
//            BufferedWriter bufWriter = new BufferedWriter(filerWriter);  
//            bufWriter.write(data);  
//            bufWriter.newLine();  
//            bufWriter.close();  
//            filerWriter.close();  
//        } catch (IOException e) {  
//            e.printStackTrace();  
//        }
//	}
//	
//	static void createDirs(String path) throws RuntimeException{
//		if(path==null||path.equals("")){
//			throw new RuntimeException("文件路径为空");
//		}
//		try {
//			File file = new File(path);
//			if(!file.exists()){
//				file.mkdirs();
//			}else {
//				if (clean) {
//					return;
//				}
//		        FilenameFilter fileFilter = new FilenameFilter() {
//		            public boolean accept(File dir, String name) {
//		                return name.endsWith(".log");
//		            }
//		        };
//		        String[]  mFileNameInDir = file.list(fileFilter);
//		        if (mFileNameInDir.length == 0) {
//		            return;
//		        }
//		        int fileIndex = 0;
//		        
//		        Date date = new Date();
//	        	Date stopDay = new Date(date.getTime()-Long.parseLong(LogInfo.LOG_MAX_FILENUM)*3600*24*1000);
//	        	String stopDayStr = new SimpleDateFormat("yyyyMMdd",Locale.getDefault()).format(stopDay);
//		        for(fileIndex = 0;fileIndex < mFileNameInDir.length;fileIndex++){
//		        	String fileName =  mFileNameInDir[fileIndex];
//		        	Log.i("SytLog", "文件名称"+fileName);
//		        	String fileDate = (fileName.split("\\.")[0]).split("_")[1];
//		        	if (stopDayStr.compareTo(fileDate) >0) {
//						File delFile = new File(path+File.separator+fileName);
//						delFile.delete();
//						Log.i("SytLog", "日志文件删除"+fileName);
//					}
//		        }
//		        clean = true;
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//			throw new RuntimeException("文件路径为空");
//		}
//	}
}

